<template>
	<view>
		<cu-custom bgColor="bg-gradual-blue" :isBack="false">
			<block slot="content">实盘</block>
		</cu-custom>
		<scroll-view scroll-x class="bg-white nav">
			<view class="flex text-center">
				<view class="cu-item flex-sub" :class="index==TabCur?'text-blue cur':''" v-for="(item,index) in navList" :key="index"
				 @tap="tabSelect(index)">
					{{item.name}}
				</view>
			</view>
		</scroll-view>
		<view class="content">
			<view v-show="TabCur === 0">
				<gendan  :isLoginOn='isLoginOn' :summaryList="summaryList"></gendan>
			</view>
			<view v-show="TabCur === 1">
				<addkey :isLoginOn='isLoginOn' :keyList="keyList" v-on:loadAccount='loadAccounts' ></addkey>
			</view>
		</view>		
	</view>
</template>

<script>
	import addkey from './addkey.vue'
	import gendan from './gendan.vue'
	var _self
	export default {
		components: {
			addkey,
			gendan			
		},
		data() {
			return {
				TabCur: 0,
				scrollLeft: 0,
				isLoginOn: false,
				navList: [{
						name: "合约"
					},
					{
						name: "接入"
					},
				],
				keyList: null,				
				summaryList: null
			}
		},
		onLoad() {
			_self = this
			_self.isLoginOn = _self.checkLogin();
			if (_self.isLoginOn)
				_self.loadSummaryList()
			else
				_self.loadUnAuthSummaryList()
		},

		onShow() {
			_self.isLoginOn = _self.checkLogin();
			if (_self.isLoginOn)
				_self.loadSummaryList()
			else
				_self.loadUnAuthSummaryList()

		},
		methods: {
			tabSelect(index) {
				console.log(index)
				this.TabCur = index;
				this.scrollLeft = (index - 1) * 60
				if (this.TabCur == 1) {
					_self.loadAccounts()
				} else {
					if (_self.isLoginOn)
						_self.loadSummaryList()
					else
						_self.loadUnAuthSummaryList()
				}
			},			
			loadAccounts() {
				console.log('load accounts ')
				uni.request({
					url: this.api + '/credentials/accounts',
					method: 'GET',
					header: {
						"Authorization": "Bearer " + this.getToken()
					},
					success: res => {
						console.log('load accounts resp')
						console.log(res.data)
						_self.keyList = res.data.data
					}
				});
			},
			loadSummaryList() {
				console.log('loadSummary List')
				uni.request({
					url: this.api + '/realdeals/summary',
					method: 'GET',
					header: {
						"Authorization": "Bearer " + this.getToken()
					},
					success: res => {
						console.log('loadSummary List resps')
						console.log(res.data)
						_self.summaryList = res.data.data
					}
				});
			},
			loadUnAuthSummaryList() {
				console.log('loadSummary List')
				uni.request({
					url: this.api + '/realdeals/unauth/summary',
					method: 'GET',
					success: res => {
						console.log('loadSummary List resps')
						console.log(res.data)
						_self.summaryList = res.data.data
					}
				});
			}
		}
	}
</script>
<style>
</style>
